// import store from "store";

// let baseUrl = 'http://192.168.0.110:18830' //请求接口地址
// let baseFlyflowUrl = 'http://192.168.0.110:18830' //请求接口地址
let baseUrl = 'http://jb.hnjubang.com/prod-api' //请求接口地址
let baseFlyflowUrl = 'http://jb.hnjubang.com' //请求接口地址
if (process.env.NODE_ENV === 'development') {
    //开发环境
    // #ifdef H5
    // baseUrl = "/api"
    // baseUrl = "http://jb.hnjubang.com/prod-api"
    //baseUrl = 'http://localhost:8080' //请求接口地址
    //baseFlyflowUrl = 'http://localhost:18830' //请求接口地址
    // #endif

    // #ifdef APP-PLUS || MP
    //baseUrl = "http://jb.hnjubang.com/prod-api"
    // #endif
} else {
    baseUrl = "http://jb.hnjubang.com/prod-api"
    baseFlyflowUrl = 'http://jb.hnjubang.com'
}
console.log(baseUrl)
const post = (url, data, isProdApi = true) => {
	return new Promise(function(resolve, reject) {
		let token = uni.getStorageSync('token');
        if(isProdApi) {
            token = 'Bearer ' + token
        }
		uni.request({
			url: (isProdApi? baseUrl : baseFlyflowUrl) + url,
			data: data,
			method: 'POST',
            header: {
                'content-type': 'application/json',
				'Accept-Language': 'zh-cn',
				'Authorization': token,
				'flyflow-tenant-id': 1
            },
            success: (res) => {
                // console.log(res);
                // console.log(res.data);
                if(res.data.code == 200) {
                    resolve(res)
                } else if(res.data.ok == true) {
                    resolve(res)
                } else {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            },
            fail(err) {
                console.log(err);
				reject(err)
			}
		})
	});
};
const put = (url, data, loading, isForm) => {
	return new Promise(function(resolve, reject) {
		const token = uni.getStorageSync('token');
		uni.request({
			url: baseUrl + url,
			data: data,
			method: 'PUT',
            header: {
                'content-type': 'application/json',
				'Accept-Language': 'zh-cn',
				'Authorization': 'Bearer ' + token,
                'flyflow-tenant-id': 1
            },
            success: (res) => {
                // console.log(res.data);
                if(res.data.code == 200) {
                    resolve(res)
                } else {
                    uni.showToast({
                        title: res.data.msg, 
                        icon: 'none'
                    })
                }
            },
            fail(err) {
                console.log(err);
				reject(err)
			}
		})
	});
};
const del = (url, data, loading, isForm) => {
	return new Promise(function(resolve, reject) {
		const token = uni.getStorageSync('token');
		uni.request({
			url: baseUrl + url,
			data: data,
			method: 'DELETE',
            header: {
                'content-type': 'application/json',
				'Accept-Language': 'zh-cn',
				'Authorization': 'Bearer ' + token
            },
            success: (res) => {
                console.log(res.data);
                if(res.data.code == 200) {
                    resolve(res)
                } else {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            },
            fail(err) {
                console.log(err);
				reject(err)
			}
		})
	});
};
const get = (url, data, isProdApi = true, loading, isForm) => {
    if (!data) {
        data = {}
    }
    let token = uni.getStorageSync('token');

    if(isProdApi) {
        token = 'Bearer ' + token
    }

    let httpDefaultOpts = {
        url: (isProdApi? baseUrl : baseFlyflowUrl) + url,
        data: data,
        method: 'GET',
        header: {
            'content-type': 'application/json;charset=utf-8',
            'Authorization': token,
            'flyflow-tenant-id': 1
        },
        timeout: 20000,
        dataType: 'json',
    }
    return new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                if (loading) {
                    uni.hideLoading();
                }
                if(res.data.code == 200) {
                    resolve(res.data)
                } else if(res.data.ok == true) {
                    resolve(res)
                } else if(res.data.code == 401) {
                    uni.showToast({
                        title: '请先登录',
                        icon: 'none',
                        duration: 2000
                    })
                    setTimeout(() => {
                        uni.redirectTo({
                            url: '/pages/login/index'
                        })
                    }, 2000)
                } else {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            }
        ).catch(
            (response) => {
                uni.showToast({
                    title: '系统错误，请稍后再试',
                    icon: 'none'
                })
                reject(response)
            }
        )
    })
};

export default {
    post,
    get,
    put,
    del,
    baseUrl
}